﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Danhmuc;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;

namespace Danhmuc
{
    public partial class frmNhaCC : BasicForm.parent2
    {
        NhaCCInfor ObjNhacc = new NhaCCInfor();
        CtrNhaCC Ctr = new CtrNhaCC(); 
       
        enum ActionMode
        {
            NEW =0,
            EDIT=1,
            DEL=2
        }
        ActionMode AcMode = ActionMode.NEW;

        public frmNhaCC()
        {
            
            CreateWaitDialog();
            InitializeComponent();
            SetWaitDialogCaption("Đang tải dữ liệu");
            InitGrid();
            Init();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ClearForm();
            txtMaNhaCC.Properties.ReadOnly = false;
            txtMaNhaCC.Focus();
            AcMode = ActionMode.NEW;
        }

        public void ClearForm()
        {
            txtMaNhaCC.Text = string.Empty;
            txtTenNhaCC.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
            txtNguoiLHe.Text = string.Empty;
            txtSDT.Text = string.Empty;
            txtMaNhaCC.Focus();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ObjNhacc.MA_NHACC = txtMaNhaCC.Text;
            ObjNhacc.TEN_NHACC = txtTenNhaCC.Text;
            ObjNhacc.DIA_CHI = txtDiaChi.Text;
            ObjNhacc.NGUOI_LHE = txtNguoiLHe.Text;
            ObjNhacc.SO_DT = txtSDT.Text;
            if (AcMode== ActionMode.NEW) //them mới
            {
                try
                {
                    Ctr.InsertItem(ObjNhacc);
                    this.gridControl1.DataSource = Ctr.GetAll();
                    if (MessageBox.Show("Bạn muốn thêm tiếp không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ClearForm();
                        ObjNhacc = new NhaCCInfor();
                    }
                    else
                    {
                        txtMaNhaCC.Properties.ReadOnly = true;
                        AcMode = ActionMode.EDIT;
                    }
                }
                catch (Exception ex)
                {
                    txtMaNhaCC.Focus();
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else
            {
                if (AcMode == ActionMode.EDIT) //chỉnh sửa
                {
                    try
                    {
                        Ctr.UpdateItem(ObjNhacc);
                        this.gridControl1.DataSource = Ctr.GetAll();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
                else //xóa
                {
                    try
                    {
                        Ctr.DeleteItem(ObjNhacc);
                        this.gridControl1.DataSource = Ctr.GetAll();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        public void Init()
        {
            //kiểm tra nếu là textbox thì cho phép di chuyển tới control ke tiep bang phim enter.
            foreach (Control item in panelControl1.Controls)
            {
                if (item.GetType() == typeof(TextEdit))
                {
                    ((TextEdit)item).EnterMoveNextControl = true;
                }
            }
            txtMaNhaCC.Focus();
            txtMaNhaCC.Properties.CharacterCasing = CharacterCasing.Upper;
        }

        public void InitGrid()
        {
            // gridView1
            // 
            GridColumn[] Cols = new GridColumn[5];
            // 
            // gridColumn1
            // 
            int i = 0;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Mã Nhà CC ";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 0;
            Cols[i].FieldName = "MA_NHACC";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Caption = "Tên nhà cung cấp";
            Cols[i].Width = 200;
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "TEN_NHACC";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Người Liên Hệ";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGUOI_LHE";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Số Điện Thoại";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "SO_DT";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Địa Chỉ";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "DIA_CHI";

            //GridColumn gridColumn2 = new GridColumn();
            this.gridView1.Columns.AddRange(Cols);
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.IndicatorWidth = 30;
            //this.gridView1.OptionsSelection.
            //this.gridView1.
            this.gridControl1.DataSource = Ctr.GetAll();
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            NhaCCInfor ObjNhacc = (NhaCCInfor)gridView1.GetRow(e.RowHandle);
            //ObjNhacc = Ctr.GetByID(SelectRow["MA_NHACC"].ToString());

            //fill du lieu len form
            txtMaNhaCC.Text = ObjNhacc.MA_NHACC;
            txtMaNhaCC.Properties.ReadOnly = true;
            txtTenNhaCC.Text = ObjNhacc.TEN_NHACC;
            txtDiaChi.Text = ObjNhacc.DIA_CHI;
            txtNguoiLHe.Text = ObjNhacc.NGUOI_LHE;
            txtSDT.Text = ObjNhacc.SO_DT;
            AcMode = ActionMode.EDIT;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            NhaCCInfor ObjNhacc = (NhaCCInfor)gridView1.GetRow(e.FocusedRowHandle);
            //ObjNhacc = Ctr.GetByID(SelectRow["MA_NHACC"].ToString());

            //fill du lieu len form
            txtMaNhaCC.Text = ObjNhacc.MA_NHACC;
            txtMaNhaCC.Properties.ReadOnly = true;
            txtTenNhaCC.Text = ObjNhacc.TEN_NHACC;
            txtDiaChi.Text = ObjNhacc.DIA_CHI;
            txtNguoiLHe.Text = ObjNhacc.NGUOI_LHE;
            txtSDT.Text = ObjNhacc.SO_DT;
            AcMode = ActionMode.EDIT;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            txtMaNhaCC.Properties.ReadOnly = true;
            txtTenNhaCC.Focus();
            AcMode = ActionMode.EDIT;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thêm tiếp không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                AcMode = ActionMode.DEL;
                simpleButton4.PerformClick();
            }
        }

        private void txtSDT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //nhấn enter ở contrl nhập liệu cuối cùng
            {
                simpleButton4.PerformClick();
            }
        }
    }
}
